package com.yc.Testjvm.cas;

/**
 * 多线程同步经典例子：银行账户存取
 * 操作：
 * 存10000次，每次+1
 * 取10000次，每次-2
 * 正确预期结果：count=0
 * 实际结果：不可预知
 * @author yc
 *
 */
public class Demo {
	
	//账户初始金额100
	public static int count = 10000;

	public static void main(String[] args) {
		System.out.println("账户初始金额" + count + "，存取完后预期0");
		getThread getT = new getThread();
		setThread setT = new setThread();
		getT.start();
		setT.start();
		try {
			getT.join();
			setT.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("存取完后实际金额为：" + count);
		
		
	}

}

class getThread extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 10000; i++) {
			Demo.count -= 2;
			
		}
	}
	
}

class setThread extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 10000; i++) {
			Demo.count++;
			
		}
	}
	
}